package com.example.zte.myapplication.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zte.myapplication.R;
import com.example.zte.myapplication.entity.Language;

import java.util.List;

/**
 * Created by ZTE on 2021/5/8.
 */

public class My2Adapter extends BaseAdapter {
    List<Language> list;
    private Context context;

    public My2Adapter(Context context, List<Language> list) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView==null) {
            // 初始化布局
            View view = LayoutInflater.from(context).inflate(R.layout.list_item_language, null);

            TextView textView = (TextView)view.findViewById(R.id.name);
            ImageView imageView =(ImageView)view.findViewById(R.id.icon);
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.imageView = imageView;
            viewHolder.textView = textView;
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context,((TextView)v).getText(),Toast.LENGTH_SHORT).show();
                }
            });
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i("ddddd","onclick");
                    Toast.makeText(context,v.getId() + "",Toast.LENGTH_SHORT).show();
                }
            });
            Language map = list.get(position);
            textView.setText(map.getText());
            imageView.setImageResource(map.getImgId());
            view.setTag(viewHolder);
            return view;

        }else{
            ViewHolder viewHolder = (ViewHolder) convertView.getTag();
            Language map = list.get(position);
            viewHolder.textView.setText(map.getText());
            viewHolder.imageView.setImageResource(map.getImgId());
            return convertView;
        }

    }

    class ViewHolder{
        TextView textView;
        ImageView imageView;
    }
}
